import { HvigorNode, HvigorTask } from "@ohos/hvigor";
import { Target, OhosPluginId, OhosHarContext } from "@ohos/hvigor-ohos-plugin";
import { generator } from "./generator";

export function RouterHarGeneratorTask(
  target: Target,
  node: HvigorNode
): HvigorTask {
  const targetName = target.getTargetName();
  return {
    name: `${targetName}@RouterHarGeneratorTask`,
    run: () => {
      doTaskAction(target, node);
    },
    dependencies: [`${target.getTargetName()}@PreBuild`],
    postDependencies: [`${target.getTargetName()}@MergeProfile`]
  };
}

function doTaskAction(target: Target, node: HvigorNode) {
  const harContext = node.getContext(
    OhosPluginId.OHOS_HAR_PLUGIN
  ) as OhosHarContext;

  generator(harContext, node, OhosPluginId.OHOS_HAR_PLUGIN);
}
